<?php

namespace App\Http\Controllers\User;

use App\Http\Classes\AvatarUpload;
use App\Http\Classes\Common;
use App\Http\Classes\User;
use App\Http\Classes\UserLog;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class InfoController extends Controller
{

    /*
     * 验证旧密码修改密码
     * POST
     * oldpassword  newpassword
     */
    public function updatePaaword(Request $request)
    {
        try
        {
            $data = $request->all();
            if ( isset($data['oldpassword']) && isset($data['newpassword']) )
            {
                if ( Common::isPassword($data['newpassword']) )
                {
                    $user = new User($_COOKIE['USER_ID']);

                    if ( $user->updatePasswordByOld($data['oldpassword'], $data['newpassword']) )
                    {
                        return $this->response(1, '修改密码成功', []);
                    }
                    else
                    {
                        return $this->response(0, '旧密码不匹配', $data['oldpassword'], $user->getMessage());
                    }
                }
                else
                {
                    return $this->response(0, '密码不规范（6-16数字字母符号）', []);
                }
            }
            else
            {
                return $this->response(0, '缺省参数', []);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 修改名称
     * POST
     * name
     */
    public function updateName(Request $request)
    {
        try
        {
            $data = $request->all();
            if ( isset($data['name']) )
            {
                $user = new User($_COOKIE['USER_ID']);
                if ( $user->updateName($data['name']) )
                {
                    return $this->response(1, '修改成功', $data['name'] );
                }
                else
                {
                    return $this->response(0, '修改失败', [], $user->getMessage());
                }
            }
            else
            {
                return $this->response(0, '缺省参数', []);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 增加用户学习时间
     * POST
     * minutes
     */
    public function increaseStudyTime(Request $request)
    {
        try
        {
            $data = $request->all();
            if ( isset($data['minutes']) )
            {
                if ( $data['minutes'] >= 0 && $data['minutes'] <= 60 )
                {
                    $user = new User($_COOKIE['USER_ID']);

                    if ($user->increaseStudyTime($data['minutes']))
                    {
                        return $this->response(1, '修改成功', $data['minutes']);
                    }
                    else
                    {
                        return $this->response(0, '修改成功', $data['minutes'], $user->getMessage());
                    }
                }
                else
                {
                    return $this->response(0, '超过0~60的范围', $data['minutes']);
                }
            }
            else
            {
                return $this->response(0, '缺省参数', []);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 更新用户头像
     * POST
     * avatar (file)
     */
    public function updateHead(Request $request)
    {
        try
        {
//            dd($request);
            if ($request->hasFile('avatar'))
            {
                $avatar = new AvatarUpload('avatar');

                if ( $avatar->upload() )
                {
                    $avatar = $avatar->getSavePaths();
                }
                else
                {
                    return $this->response(0, '文件上传时发生错误', [], $avatar->getErrorMessage());
                }

                $avatar = $avatar[0];

                $user = new User($_COOKIE['USER_ID']);

                if ( $user->updateHead($avatar) )
                {
                    $avatar = HOST . $avatar;
                    return $this->response(1, '修改成功', ['head' => $avatar]);
                }
                else
                {
                    return $this->response(0, '修改失败', [], $user->getMessage());
                }
            }
            else
            {
                return $this->response(0, '缺省参数', []);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 增加用户已学课件数， 每次加1
     * GET
     */
    public function increaseLessonCount()
    {
        try
        {
            $user = new User($_COOKIE['USER_ID']);

            if ( $user->increaseLessonCount() )
            {
                return $this->response(1, '修改成功', []);
            }
            else
            {
                return $this->response(0, '修改失败', [], $user->getMessage());
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 修改用户年级
     * POST
     * grade(int)
     * 1  初一
     * 2  初二
     * 3  初三
     */
    public function updateGrade(Request $request)
    {
        try
        {
            if ( $request->has('grade') )
            {
                $allow = [1,2,3];
                $grade = $request->get('grade');
                if ( in_array($grade, $allow) )
                {
                    $user = new User($_COOKIE['USER_ID']);

                    if ( $user->updateGrade($grade) )
                    {
                        return $this->response(1, '修改成功', ['grade' => $grade]);
                    }
                    else
                    {
                        return $this->response(0, '修改失败', [], $user->getMessage());
                    }
                }
                else
                {
                    return $this->response(0, '超过了允许的范围');
                }
            }
            else
            {
                return $this->response(0, '缺少参数');
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 更改用户播放设置
     * POST
     * playmod(int)
     */
    public function updatePlayMod(Request $request)
    {
        try
        {
            if ( $request->has('playmod') )
            {
                $allow = [1,2];
                $playmod = $request->get('playmod');

                if ( in_array($playmod, $allow) )
                {
                    $user = new User($_COOKIE['USER_ID']);

                    if ( $user->updatePlayMod($playmod) )
                    {
                        return $this->response(1, '修改成功', ['playmod' => $playmod]);
                    }
                    else
                    {
                        return $this->response(0, '修改失败', [], $user->getMessage());
                    }
                }
                else
                {
                    return $this->response(0, '超过了允许的范围');
                }
            }
            else
            {
                return $this->response(0, '缺少参数');
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 更改用户播放时间设置
     * POST
     * playtime
     */
    public function updatePlayTime(Request $request)
    {
        try
        {
            if ( $request->has('playtime') )
            {
                $allow = [1,2];
                $playtime = $request->get('playtime');

                if ( in_array($playtime, $allow) )
                {
                    $user = new User($_COOKIE['USER_ID']);

                    if ( $user->updatePlayTime($playtime) )
                    {
                        return $this->response(1, '修改成功', ['playtime' => $playtime]);
                    }
                    else
                    {
                        return $this->response(0, '修改失败', [], $user->getMessage());
                    }
                }
                else
                {
                    return $this->response(0, '超过了允许的范围');
                }
            }
            else
            {
                return $this->response(0, '缺少参数');
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 更改用户语句模式设置
     * POST
     * sentencemod
     */
    public function updateSentenceMod(Request $request)
    {
        try
        {
            if ( $request->has('sentencemod') )
            {
                $allow = [1,2];
                $sentencemod = $request->get('sentencemod');

                if ( in_array($sentencemod, $allow) )
                {
                    $user = new User($_COOKIE['USER_ID']);

                    if ( $user->updateSentenceMod($sentencemod) )
                    {
                        return $this->response(1, '修改成功', ['sentencemod' => $sentencemod]);
                    }
                    else
                    {
                        return $this->response(0, '修改失败' . $user->getMessage(), []);
                    }
                }
                else
                {
                    return $this->response(0, '超过了允许的范围');
                }
            }
            else
            {
                return $this->response(0, '缺少参数');
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 用户更改限定时间学习计划
     * POST
     * time
     * word
     * sentence
     */
    public function updatePlan(Request $request)
    {
        try
        {
            if ( $request->has('time') && $request->has('word') && $request->has('sentence') )
            {
                $data = [];
                $data['plan_hours'] = $request->get('time');
                $data['word_counting'] = $request->get('word');
                $data['sentence_counting'] = $request->get('sentence');

                $user = new User($_COOKIE['USER_ID']);

                if ( $user->updatePlan($data) )
                {
                    return $this->response(1, '修改成功！', $data);
                }
                else
                {
                    return $this->response(0, $user->getMessage(), []);
                }
            }
            else
            {
                return $this->response(0, '缺少参数');
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 获取用户信息
     * GET
     *
     */
    public function getRecord()
    {
        try
        {
            $user = new User();

            $userRecord = $user->getRecord($_COOKIE['USER_ID']);

            if ($userRecord)
            {
                return $this->response(1, '获取成功！', $userRecord);
            }
            else
            {
                return $this->response(0, $user->getMessage(), []);
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 增加用户当日的学习时长
     * POST
     * time int
     */
    public function increaseDaliyTime(Request $request)
    {
        try
        {
            if ($request->has('time'))
            {
                $time = $request->get('time');

                if ( ! is_numeric($time) )
                {
                    return $this->response(0, '非法参数');
                }

                $user = new UserLog($_COOKIE['USER_ID']);

                $flag = $user->increaseDaliyTime($time);

                if ( $flag )
                {
                    return $this->response(1, '更新成功');
                }
                else
                {
                    return $this->response(0, '更新失败', [], $user->getMessage());
                }
            }
            else
            {
                return $this->response(0, '缺少参数');
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 增加用户当日的学习单词数
     * POST
     * words int
     */
    public function increaseDaliyWord(Request $request)
    {
        try
        {
            if ( $request->has('words') )
            {
                $count = $request->get('words');

                if ( ! is_numeric($count) )
                {
                    return $this->response(0, '非法参数');
                }

                $user = new UserLog($_COOKIE['USER_ID']);

                $flag = $user->increaseDaliyWord($count);

                if ( $flag )
                {
                    return $this->response(1, '更新成功');
                }
                else
                {
                    return $this->response(0, '更新失败', [], $user->getMessage());
                }
            }
            else
            {
                return $this->response(0, '缺少参数');
            }
        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }

    /*
     * 获取当日的学习记录
     * GET
     */
    public function getDaliyLog()
    {
        try
        {
            $user = new UserLog($_COOKIE['USER_ID']);

            $log = $user->getDaliyLog();

            if ($log)
            {
                return $this->response(1, '获取成功', $log);
            }
            else
            {
                return $this->response(0, '无匹配数据', [], $user->getMessage());
            }

        }
        catch (\Exception $exception)
        {
            return $this->exceptionResponse($exception, __FUNCTION__);
        }
    }
}
